﻿using Flurl.Http;
using Pay.Core;
using Pay.DTO.tenPay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service.Pay
{
    public class TenSubPaySign
    {
#if DEBUG
        //public string api_url = "https://api.hthao.com/paychannel";
        public string api_url = "http://120.77.31.188:8111/paychannel";
        public string public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTLuh7fWOwlnhFCbhzjjDzjNux4P8bW7Il9G5RBYA+ScbhMsR+uKhHbBg111Go4SuKAevjl1zCKtHIg6HYRrtjRFCftSVvSro6FDIE+tURZ1N4+nOO/JJL1bI0pwUD5z5HVuGTWsTK50YGL9O2JEthj1yrL8AETZTOhXtshBPFSwIDAQAB";
        public string private_key = "MIICXgIBAAKBgQC+K69/+NfMSgEbMQHcNfr0K0HmcngjglZFMXF3bI0dP1OdjlZ8qp7zWw/dd4d/qlL5cyxKwWo7JeqQoLrhQBs2AxVvDVAnYDURWmRDQx1eUDcDHCjMq3Co51uMz9XXDpm1EsfqiUzRs7C5E1UKjSQ5gB1Z9YijMyPWyD9p+plScwIDAQABAoGBALFhPBSIvniduoRrZMnLS+W+B3z03px4cRGfOa/j/4f9TnfSkVc9Kn/hO14epLNu4nAsfm7IE9dR2YXHdiiuS/GoyMMK0eoRfo+rghSAxuKN37Y3Yfs+uxNiVsdPBAFQFrSKieuw2n680rlh3YwwtctHRzEr9Qdp5oVtFvQjKWFBAkEA/VRE33ZIKx7fsvgttH/pRX4n08Z2bELPWuOpog/mQEiZwqu/I2y9G2V4spmgyaqzphPjaTZ3T4bO+k2RcikckwJBAMAs8+f35So8zAkAIR7Rgbx+8CI8bPviCVFkw05KaOnbLHyA7908sDxOOVgOHFVDQa1ZuLyZDKxN9lSEFt1afqECQQDIzjyN2aeZLq7poZoxItdY84hEcWO7qzXMePwk7293c1UWn6cyQGaiScvj3q06Tip7Mfaolrj6fASjGkt5b6crAkAz4C6bEKzY4P6G83T9V7q4wl9aLouQ1uyC/ztFeAqoDhUNKtUVGJvOO6N5JJ37WiNxgwW+S2ZDYOw81kdBy3rhAkEA47yZYf1vwP83bqm2oocTNo8Ym1NoD2t/ZbHLLOC8QWFkwe4HD9EudFzGOpDRL+rczujEO679iPPRse8zbaPlXw==";
        public const string mchid = "684116541210";
        public const string path = @"c:\pro\ctrip_rsa.pfx";
#else
        //public string api_url = "https://api.hthao.com/paychannel";
        public string api_url = "http://120.77.31.188:8111/paychannel";
        public string public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTLuh7fWOwlnhFCbhzjjDzjNux4P8bW7Il9G5RBYA+ScbhMsR+uKhHbBg111Go4SuKAevjl1zCKtHIg6HYRrtjRFCftSVvSro6FDIE+tURZ1N4+nOO/JJL1bI0pwUD5z5HVuGTWsTK50YGL9O2JEthj1yrL8AETZTOhXtshBPFSwIDAQAB";
        public string private_key = "MIICXgIBAAKBgQC+K69/+NfMSgEbMQHcNfr0K0HmcngjglZFMXF3bI0dP1OdjlZ8qp7zWw/dd4d/qlL5cyxKwWo7JeqQoLrhQBs2AxVvDVAnYDURWmRDQx1eUDcDHCjMq3Co51uMz9XXDpm1EsfqiUzRs7C5E1UKjSQ5gB1Z9YijMyPWyD9p+plScwIDAQABAoGBALFhPBSIvniduoRrZMnLS+W+B3z03px4cRGfOa/j/4f9TnfSkVc9Kn/hO14epLNu4nAsfm7IE9dR2YXHdiiuS/GoyMMK0eoRfo+rghSAxuKN37Y3Yfs+uxNiVsdPBAFQFrSKieuw2n680rlh3YwwtctHRzEr9Qdp5oVtFvQjKWFBAkEA/VRE33ZIKx7fsvgttH/pRX4n08Z2bELPWuOpog/mQEiZwqu/I2y9G2V4spmgyaqzphPjaTZ3T4bO+k2RcikckwJBAMAs8+f35So8zAkAIR7Rgbx+8CI8bPviCVFkw05KaOnbLHyA7908sDxOOVgOHFVDQa1ZuLyZDKxN9lSEFt1afqECQQDIzjyN2aeZLq7poZoxItdY84hEcWO7qzXMePwk7293c1UWn6cyQGaiScvj3q06Tip7Mfaolrj6fASjGkt5b6crAkAz4C6bEKzY4P6G83T9V7q4wl9aLouQ1uyC/ztFeAqoDhUNKtUVGJvOO6N5JJ37WiNxgwW+S2ZDYOw81kdBy3rhAkEA47yZYf1vwP83bqm2oocTNo8Ym1NoD2t/ZbHLLOC8QWFkwe4HD9EudFzGOpDRL+rczujEO679iPPRse8zbaPlXw==";
        public const string mchid = "684116541210";
        public const string path = @"d:\cer\ctrip_rsa.pfx";
#endif

        public async Task<T> PayGateway<T>(ten_sub_req request, string url) where T : ten_sub_res, new()
        {

            var log = IocHelper.Get<ILogHelper>("info");

            try
            {
                string json = request.ToJson();
                log.Info(json);
                string signValue = _sign(json);
                log.Info(signValue);
                var result = await (api_url + url)
                    .WithHeader("x-payapi-signNumber", mchid)
                    .WithHeader("x-payapi-signType", "SHA256withRSA")
                    .WithHeader("x-payapi-signValue", signValue)
                    .WithHeader("x-payapi-signTimestamp", DateTime.Now.ToString("yyyyMMddHHmmss"))
                    .WithHeader("charset", "UTF-8")
                    .PostJsonAsync(request).ReceiveString();
                log.Info(result);
                return result.ToObject<T>();
            }
            catch (FlurlHttpException ex)
            {
                log.Error(ex.Message);
                return null;
            }
        }

        private string _sign(string jsonData)
        {
            string sign = RSAFromPkcs.Sha256Sign(jsonData, path, "");
            return sign;
        }

        public string encrypt(string inputStr)
        {
            string sign = RSAFromPkcs.RsaEncryptData(inputStr, public_key, "utf-8", 245);
            return sign;
        }
    }
}
